import axios from "axios";
import { ElMessage } from "element-plus";
// import { openLoading, closeLoading } from '../loading'

//post请求头
//axios.defaults.headers.post["Content-Type"] ="application/x-www-form-urlencoded;charset=UTF-8";

let loading:any = null

const serviceAxios = axios.create({
  baseURL: '',
  withCredentials: true,
  timeout: 10000,
  headers: {
    "Content-Type": "application/json; charset=UTF-8;"
  }
})

serviceAxios.interceptors.request.use(
  (config:any) => {
    // 业务逻辑
    // openLoading()
    config.headers.Authorization = localStorage.getItem("token")
    return config;
  },
  error => {
    return Promise.reject(error);
  }
);

serviceAxios.interceptors.response.use(
  response => {
    // closeLoading()
    if (response.status == 200) {
      return Promise.resolve(response.data);
    } else {
      return Promise.reject(response);
    }
  },
  error => {
    // closeLoading()
    ElMessage.error(`异常请求：${JSON.stringify(error.message)}`)
  }
);

export default serviceAxios;